package com.trkj.usercenter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.trkj.base.AppManager;
import com.trkj.base.BaseActivity;
import com.trkj.base.Constants;
import com.trkj.base.OnResponseResultListener;
import com.trkj.base.newwork.HttpRequest;
import com.trkj.base.newwork.PostWaitingDataFromRemote;
import com.trkj.base.utils.UserService;
import com.trkj.push.Push;
import com.trkj.taoruiyun.MainActivity;
import com.trkj.taoruiyun.R;

/**
 * Created by LiZanhong on 2015/06/21.
 */
public class LoginActivity extends BaseActivity {
    public static final String LOGIN_FAILURE_CONTENT = "对不起，登陆失败，请检查用户名或密码";

    @ViewInject(R.id.user_name)
    private EditText userName;

    @ViewInject(R.id.password)
    private EditText password;

    private HttpRequest httpRequest;
    private UserService userService;

    public static final String ACTION = "com.trkj.usercenter.LoginActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        ViewUtils.inject(this);
        httpRequest = new HttpRequest();
        userService = new UserService(this);

        clear();
    }



    protected void clear(){
        Activity activity = AppManager.getAppManager().getActivity(MainActivity.class);
        if(null != activity){
            MainActivity mainActivity = (MainActivity) activity;
            mainActivity.clearSubFragmentData();
        }
    }

    protected  void openMainActivity(){
        Activity activity = AppManager.getAppManager().getActivity(MainActivity.class);
        if(null != activity){
            MainActivity mainActivity = (MainActivity) activity;
            Intent intent = new Intent(MainActivity.ACTION);
        }
    }

    @Override
    protected String getCustomerTitle() {
        return "韬睿科技学习平台";
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == android.R.id.home) {
            AppManager.getAppManager().AppExit(this);
        }
        return true;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            AppManager.getAppManager().AppExit(this);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 登陆
     * @param v
     */
    public void login(View v){
        final String strPassword = password.getText().toString();
                String param = "userName="
                + userName.getText().toString()
                + "&password=" + strPassword;

        PostWaitingDataFromRemote dataFromRemote = new PostWaitingDataFromRemote(this);
        dataFromRemote.setResponseResultListener(new OnResponseResultListener() {
            @Override
            public void onResponseResult(String result) {
                JSONObject object = JSON.parseObject(result);
                if(object != null){
                    boolean success = object.getBoolean("success");
                    if(success){
                        JSONObject user = object.getJSONObject("user");
                        if(user.getIntValue("user_type") == 1){
                            toast("您是管理员用户，无法使用手机客户端");
                        }else {
                            userService.saveUser(user);
                            userService.savePassword(strPassword);
                            new Push(LoginActivity.this).setTags();
                            setResult(Activity.RESULT_OK);
                            LoginActivity.this.finish();
                        }
                    }else{
                        toast(LOGIN_FAILURE_CONTENT);
                    }
                }else{
                    toast(LOGIN_FAILURE_CONTENT);
                }
            }
        });

        dataFromRemote.execute(Constants.ApiUrl.LOGIN, param);
    }
}
